package com.lanhe.common.utils.duoyou;


import cn.hutool.http.HttpRequest;
import com.lanhe.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 多游
 *
 * @author zhanggl
 * @date 2023/12/4
 */
@Component
@Slf4j
public class DuoYouUtil {

	@Value("${lanhe.duoyou.mediaId}")
	private String mediaId;

	@Value("${lanhe.duoyou.appSecret}")
	private String appSecret;

	@Value("${lanhe.duoyou.listURL}")
	private String listURL;

	@Value("${lanhe.duoyou.confURL}")
	private String confURL;

	@Value("${lanhe.duoyou.infoURL}")
	private String infoURL;

	@Value("${lanhe.duoyou.accountURL}")
	private String accountURL;

	@Value("${lanhe.duoyou.tasksURL}")
	private String tasksURL;

	@Value("${lanhe.duoyou.searchURL}")
	private String searchURL;

	@Value("${lanhe.duoyou.recommendsURL}")
	private String recommendsURL;

	@Value("${lanhe.duoyou.attendsURL}")
	private String attendsURL;

	@Value("${lanhe.duoyou.bannerURL}")
	private String bannerURL;

	/**
	 * 广告列表
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertIds  广告ID列表（可不传）多个广告ID以逗号分割
	 * @param type       列表数据类型（默认为all）
	 * @param appChannel 媒体包渠道ID（仅安卓用）
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getDuoYouList(String userId,
	                            String deviceIds,
	                            String deviceType,
	                            String advertIds,
	                            String type,
	                            String appChannel,
	                            Integer pageNum,
	                            Integer pageSize) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = listURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&advert_ids=" + advertIds + "&type=" + type + "&app_channel=" + appChannel + "&page="
				+ pageNum + "&size=" + pageSize + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();

		log.info("[{}] [getDuoYouList] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getDuoYouList] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getDuoYouList] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 广告列表-详情
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getDuoYouInfo(String userId,
	                            String deviceIds,
	                            String deviceType,
	                            String advertId) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		formSign.put("device_ids", deviceIds);
		formSign.put("device_type", deviceType);

		String sign = StringUtils.generateSignature(formSign, appSecret);
		long startTime = System.currentTimeMillis();
		String API_URL = infoURL + "/" + advertId + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getDuoYouInfo] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getDuoYouInfo] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getDuoYouInfo] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 游戏账号数据
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getAccountInfo(String userId,
	                            String deviceIds,
	                            String deviceType,
	                            String advertId) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		formSign.put("device_ids", deviceIds);
		formSign.put("device_type", deviceType);

		String sign = StringUtils.generateSignature(formSign, appSecret);
		long startTime = System.currentTimeMillis();
		String API_URL = accountURL + "/" + advertId + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getAccountInfo] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getAccountInfo] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getAccountInfo] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 广告列表-任务
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getTasks(String userId,
	                       String deviceIds,
	                       String deviceType,
	                       String advertId) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		formSign.put("device_ids", deviceIds);
		formSign.put("device_type", deviceType);

		String sign = StringUtils.generateSignature(formSign, appSecret);
		long startTime = System.currentTimeMillis();
		String API_URL = tasksURL + "/" + advertId + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getTasks] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getTasks] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getTasks] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}


	/**
	 * 广告列表
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getAttends(String userId,
	                         String deviceIds,
	                         String deviceType,
	                         Integer pageNum,
	                         Integer pageSize) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = attendsURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&page=" + pageNum + "&size=" + pageSize + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getAttends] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getAttends] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getAttends] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}


	/**
	 * 搜索列表
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getSearch(String userId,
	                        String deviceIds,
	                        String deviceType,
	                        Integer pageNum,
	                        Integer pageSize,
	                        String k) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = searchURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&page=" + pageNum + "&size=" + pageSize + "&sign=" + sign + "&extra=" + userId+ "&name=" + k;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getSearch] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getSearch] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getSearch] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 推荐列表
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getRecommends(String userId,
	                            String deviceIds,
	                            String deviceType,
	                            Integer pageNum,
	                            Integer pageSize) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = recommendsURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&page=" + pageNum + "&size=" + pageSize + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getRecommends] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getRecommends] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getRecommends] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}



	/**
	 * 推荐列表--Banner图片
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getBanners(String userId,
	                            String deviceIds,
	                            String deviceType,
	                            Integer pageNum,
	                            Integer pageSize) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = bannerURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&page=" + pageNum + "&size=" + pageSize + "&sign=" + sign + "&extra=" + userId;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getBanners] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getBanners] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getBanners] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}


	/**
	 * 用户信息
	 *
	 * @param userId     用户ID
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getConf(String userId,
	                      String deviceIds,
	                      String deviceType) throws Exception {
		Map<String, String> formSign = new HashMap<>();
		formSign.put("media_id", mediaId);
		formSign.put("user_id", userId);
		if (StringUtils.isNotBlank(deviceIds)) {
			formSign.put("device_ids", deviceIds);
		}
		if (StringUtils.isNotBlank(deviceType)) {
			formSign.put("device_type", deviceType);
		}
		String sign = StringUtils.generateSignature(formSign, appSecret);

		long startTime = System.currentTimeMillis();
		String API_URL = confURL + "?media_id=" + mediaId + "&user_id=" + userId + "&device_ids=" + deviceIds + "&device_type="
				+ deviceType + "&sign=" + sign;
		String result = HttpRequest.get(API_URL)
				.execute().body();
		log.info("[{}] [getConf] ======>请求 {}", this.getClass().getSimpleName(),
				API_URL);
		log.info("[{}] [getConf] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getConf] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 获取加密字符串
	 *
	 * @param params 条件
	 * @return 加密字符串
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	public String getSign(Map<String, String> params) throws Exception{
		params.put("media_id",mediaId);
		return StringUtils.generateSignature(params, appSecret);
	}
}
